<?php
/**
 * 会员系统    暂时不支持 系统组授权
 * groupid系统组,verify为验证级别 ,coupons为自定义会员组
 * 功能:会员的添加、删除、查看信息、授权
 *@author wolf  [Email: 116311316@qq.com]
 *@since 2011-07-20
 *@version 1.0
 */
class MemberController extends NodeController {
	
	// 配置用户列表 导入
	public function listing() {
		$cid = 1;
		/* 分页如何缓存 */
		if (isset ( $_GET ['p'] )) {
			$cache_id = 'p' . $_GET ['p'];
		} else {
			$cache_id = $cid;
		}
		$member = self::getMemberService ()->listing ( $_GET ['p'], $_GET );
		$verify = isset ( $_GET ['verify'] ) ? 0 : $_GET ['verify'];
		$this->view ()->assign ( 'verify', array ('id' => $verify, 'name' => $member ['verify'] [$verify] ) );
		$groupid = isset ( $_GET ['gruopid'] ) ? 0 : $_GET ['groupid'];
		$this->view ()->assign ( "groupid", array ('id' => $groupid, 'name' => $member ['groupname'] [$groupid] ) );
		$this->view ()->assign ( 'num', $member ['page'] );
		$this->view ()->assign ( 'totalnum', $member ['totalnum'] );
		$this->view ()->assign ( 'news', $member ['memberlist'] );
		// 在线导出功能
		$this->view ()->assign ( 'cate', $this->getMemberService ()->_verify );
		$this->view ()->assign ( "group", $member ['groupname'] );
		if (isset ( $_GET ['mb'] )) {
			$this->view ()->display ( "file:member/morder.tpl" );
			exit ();
		}
		$this->view ()->display ( 'file:member/mlist.tpl' );
	}
	/**
	 * 删除会员
	 * Enter description here ...
	 */
	public function remove() {
		$uid = $_POST ['uid'];
		if ($uid < 1) {
			$this->sendNotice ( "uid must be greater than 1!" );
		}
		self::getMemberService ()->remove ( $uid );
		$this->sendNotice ( "del success!" );
	}
	/**
	 * 审核头像
	 * Enter description here ...
	 */
	public function vaildFace() {
		$id = $_POST ['id'];
		//验证头像
		$type = $_POST ['type'];
		$flag = $this->getMemberService ()->validFace ( $id, $type );
		if ($flag) {
			$this->sendNotice ( "success" );
		} else {
			$this->sendNotice ( "refuse" );
		}
	}
	/**
	 * 登录明细
	 * Enter description here ...
	 */
	public function loginLog() {
		$log = LogModel::instance ()->getLogByWhere ( null, 50 );
		$this->view ()->assign ( "log", $log );
		$this->view ()->clearCache ( "file:member/log.tpl" );
		$this->view ()->display ( "file:member/log.tpl" );
	}
	/**
	 * /member/setface/    filename face
	 * 流上传服务
	 */
	public function setFace() {
		$user = unserialize ( $_SESSION ['uid'] );
		$uid = $user ['uid'];
		$userinfo = MemberModel::instance ()->getOneMember ( array ('uid' => $uid ) );
		$this->getMemberService ()->uploadFace ( $uid, $userinfo ['real_name'] );
	}
	/**
	 * 头像审核列表
	 * Enter description here ...
	 */
	public function faceList() {
		$rs = self::getMemberService ()->getVaildFaceList ();
		$this->view ()->assign ( 'list', $rs );
		$this->view ()->display ( 'file:member/flist.tpl' );
	}
	/**
	 * 查询用户系统
	 */
	public function cSearch() {
		$rs = self::getMemberService ()->search ( $_POST ['type'], $_POST ['value'] );
		$this->view ()->assign ( 'news', $rs );
		$this->view ()->assign ( 'cate', self::getMemberService ()->_verify );
		if (isset ( $_POST ['mb'] )) {
			$this->view ()->display ( 'file:member/' . $_POST ['mb'] );
			exit ();
		}
		$this->view ()->display ( 'file:member/mlist.tpl' );
	}
	/**
	 * 最近30天的登录情况 图表显示
	 */
	public function loginChart() {
		$days = isset ( $_GET ['days'] ) ? $_GET ['days'] : 30;
		$format = isset ( $_GET ['format'] ) ? $_GET ['format'] : "d";
		$rs = self::getMemberService ()->getMemberLoginStatus ( $format, $days );
		$this->view ()->assign ( 'keys', $rs ['keys'] );
		$this->view ()->assign ( 'values', $rs ['values'] );
		$this->view ()->display ( 'file:member/loginchart.tpl' );
	}
	/**
	 * 对单个用户进行编辑
	 */
	public function add() {
		if ($_POST) {
			//添加用户
			//   $data = $_POST;
			$salt = $this->randstr ();
			$data ['username'] = trim ( $_POST ['username'] );
			$data ['password'] = md5 ( md5 ( $_POST ['password'] ) . $salt );
			$data ['salt'] = $salt;
			$data ['groupid'] = $_POST ['groupid'];
			$data ['add_time'] = time ();
			$data ['email'] = trim ( $_POST ['email'] );
			$data ['mobile_phone'] = trim ( $_POST ['mobile_phone'] );
			$data ['address'] = trim ( $_POST ['address'] );
			$data ['real_name'] = trim ( $_POST ['real_name'] );
			$rs = MemberModel::instance ()->getOneMember ( array ('mobile_phone' => $_POST ['mobile_phone'] ) );
			if (! empty ( $rs )) {
				echo "该手机号码已经被绑定";
				exit ();
			}
			$data ['mobile_phone'] = $_POST ['mobile_phone'];
			$data ['verify'] = $_POST ['verify']; //直接验证成功
			MemberModel::instance ()->addMember ( $data );
			$this->redirect ( '添加成功!', '/member/edit', 1 );
			exit ();
		}
		// $member = MemberModel::instance ()->getAllMemberInfo ( 5 );
		$group = MemberModel::instance ()->getGroupAll ();
		// $this->view()->assign ( 'member', $member );
		$pwd = $this->randstr ();
		$this->view ()->assign ( 'verify', self::getMemberService ()->_verify );
		$this->view ()->assign ( 'cachetype', 1 );
		$this->view ()->assign ( 'password', $pwd );
		$this->view ()->assign ( 'group', $group );
		$this->view ()->clearCache ( 'file:member/add.tpl' );
		$this->view ()->display ( 'file:member/add.tpl' );
	}
	
	public function edit() {
		if (isset ( $_GET ['id'] )) {
			$uid = trim ( $_GET ['id'] );
			$rs = MemberModel::instance ()->getOneMember ( array ('uid' => $uid ) );
		}
		if ($_POST) {
			//添加用户
			// $data = $_POST;
			//密码为空 不更改
			if (! empty ( $_POST ['password'] )) {
				$salt = $this->randstr ();
				$data ['password'] = md5 ( md5 ( $_POST ['password'] ) . $salt );
				$data ['salt'] = $salt;
			}
			// $data['coupons'] = trim($_POST['coupons']);
			$data ['email'] = trim ( $_POST ['email'] );
			$data ['mobile_phone'] = trim ( $_POST ['mobile_phone'] );
			$data ['country'] = trim ( $_POST ['country'] );
			$data ['area'] = trim ( $_POST ['area'] );
			$data ['real_name'] = trim ( $_POST ['real_name'] );
			$data ['username'] = trim ( $_POST ['username'] );
			$data ['groupid'] = $_POST ['groupid'];
			$data ['verify'] = $_POST ['verify']; //直接验证成功
			$data ['manager'] = $_POST ['manager'];
			$data ['status'] = $_POST ['status']; //直接验证成功
			//
			$uid = $_POST ['uid'];
			MemberModel::instance ()->setMemberByWhere ( $data, array ('uid' => $uid ) );
			$this->redirect ( '更改成功!', "/member/edit/?id={$uid}" );
			exit ();
		}
		$group = MemberModel::instance ()->getGroupAll ();
		$this->view ()->assign ( 'group', $group );
		$this->view ()->assign ( "verify", $this->getMemberService ()->_verify );
		$this->view ()->assign ( "rs", $rs );
		$this->view ()->assign ( 'cachetype', 1 );
		$this->view ()->clearCache ( 'file:member/edit.tpl' );
		$this->view ()->display ( 'file:member/edit.tpl' );
	}
	//加密方法
	protected function randstr($len = 6) {
		$chars = 'abcdefghijklmnopqrstuvwxyz0123456789';
		// characters to build the password from 
		mt_srand ( ( double ) microtime () * 1000000 * getmypid () );
		// seed the random number generater (must be done) 
		$password = '';
		while ( strlen ( $password ) < $len )
			$password .= substr ( $chars, (mt_rand () % strlen ( $chars )), 1 );
		return $password;
	}
	/**
	 * 用户后台中心
	 */
	public function home() {
		$this->view ()->assign ( 'error', '欢迎您!' );
		$this->order ();
		$this->view ()->clearCache ( 'mysql:home.tpl' );
		$this->view ()->display ( 'mysql:home.tpl' );
	}
	
	/**
	 * 审核
	 * Enter description here ...
	 */
	public function setStatus() {
		if (isset ( $_POST ) && ! empty ( $_POST )) {
			$this->validteNode ( SUPER );
			$data = substr ( trim ( $_POST ['ids'] ), 1 );
			// 只复制一个的情况
			$type = $_POST ['type']; //批量
			$verify = array_flip ( self::getMemberService()->_verify );
			if (strpos ( $data, "," )) {
				$data = explode ( ",", $data );
				foreach ( $data as $v ) {
					if ($type == "refuse") {
						$rs = MemberModel::instance ()->setMemberByWhere ( array ('verify' => - 1 ), array ('uid' => $v ) );
					}
					if ($type == "pass") {
						$rs = MemberModel::instance ()->setMemberByWhere ( array ('verify' => 1 ), array ('uid' => $v ) );
					}
				}
			} else {
				if ($type == "refuse") {
					$rs = MemberModel::instance ()->setMemberByWhere ( array ('verify' => - 1 ), array ('uid' => $data ) );
				}
				if ($type == "pass") {
					$rs = MemberModel::instance ()->setMemberByWhere ( array ('verify' => 1 ), array ('uid' => $data ) );
				}
			}
			$this->view ()->clearCache ( 'file:member/mlist.tpl' );
			echo json_encode ( array ('status' => true, 'message' => '操作成功!' ) );
			exit ();
		}
	}
	/**
	 * 修改密码 
	 */
	public function repassword() {
		
		$this->view ()->display ( 'file:member/password.tpl' );
	
	}
	
	public static function getMemberService() {
		return new MemberService ();
	}
}